package org.gap.wizards.world.actions;

import java.awt.Point;

import net.slashie.libjcsi.CharKey;

import org.gap.wizards.views.MessageOutput;
import org.gap.wizards.views.ViewPlatform;
import org.gap.wizards.world.ClosedSpace;
import org.gap.wizards.world.Location;
import org.gap.wizards.world.Movement;
import org.gap.wizards.world.WorldAction;

public abstract class AbstractDoorInteraction implements WorldAction {
	private final Point pcLocation;
	private final Location location;
	private final ViewPlatform console;
	private final MessageOutput output;

	public AbstractDoorInteraction(Location location, MessageOutput output, ViewPlatform console) {
		this.location = location;
		this.output = output;
		this.pcLocation = location.getCharacterLocation();
		this.console = console;
	}

	@Override
	public void perform() {
		output.addMessage(getMessage());
		CharKey key = console.getKeyPressed();
		char pressed = key.toString().charAt(0);
		if (Movement.isValid(pressed)) {
			Movement direction = Movement.from(pressed);
			Point door = direction.adjustLocation(pcLocation);
			for (Object each : location.getAllFromLocation(door)) {
				if (each instanceof ClosedSpace) {
					performActionOn((ClosedSpace)each, door);
				}
			}
		}
	}

	protected abstract String getMessage();
	
	protected abstract void performActionOn(ClosedSpace building, Point door);
}
